package com.guojiaming.shop.config;

import com.guojiaming.shop.bean.ResponseFactory;
import com.guojiaming.shop.exception.DataNotFindException;
import com.guojiaming.shop.exception.ErrorInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import static org.springframework.http.HttpStatus.NOT_EXTENDED;

/**
 * @author vivo
 */
@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);

    @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
        logger.info("发生数据验证异常", ex);
        if (ex instanceof BindException) {
            BindException exception = (BindException) ex;
            ErrorInfo errorInfo = new ErrorInfo(exception);
            return new ResponseEntity<>(ResponseFactory.failed(errorInfo.toString()), NOT_EXTENDED);
        }
        if (ex instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException exception = (MissingServletRequestParameterException) ex;
            String builder = "参数" +
                    exception.getParameterName() +
                    "错误,原因缺少该参数";
            return new ResponseEntity<>(ResponseFactory.failed(builder), NOT_EXTENDED);
        }
        return new ResponseEntity<>(ResponseFactory.failed(ex.getLocalizedMessage()), NOT_EXTENDED);
    }


    /**
     * 处理数据查询失败的异常回调
     *
     * @param e 异常信息
     * @return 错误信息
     */
    @ExceptionHandler(DataNotFindException.class)
    @ResponseBody
    public com.guojiaming.shop.bean.ResponseBody<String> dataFindExceptionHandler(Exception e) {
        logger.info("发生数据查询异常", e);
        return ResponseFactory.failed(e.getLocalizedMessage());
    }

    /**
     * 处理数据查询失败的异常回调
     *
     * @param e 异常信息
     * @return 错误信息
     */
    @ExceptionHandler(BadSqlGrammarException.class)
    @ResponseBody
    public com.guojiaming.shop.bean.ResponseBody<String> sqlExceptionHandler(Exception e) {
        if (e instanceof BadSqlGrammarException) {
            BadSqlGrammarException exception = (BadSqlGrammarException) e;
            logger.info("发生sql执行异常", exception.getSql());
            return ResponseFactory.failed("参数错误:" + exception.getSQLException().getMessage());
        }
        return ResponseFactory.failed("参数错误" + e.getMessage());
    }

    /**
     * 其他异常信息回调
     *
     * @param e 异常信息
     * @return 错误信息
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public com.guojiaming.shop.bean.ResponseBody<String> defaultExceptionHandler(Exception e) {
        logger.info("出现异常", e);
        return ResponseFactory.failed(e.getLocalizedMessage() == null ? e.getClass().getName() : e.getLocalizedMessage());
    }
}
